<?php
require_once 'Zend/Db/Table/Abstract.php';
require_once APPLICATION_PATH . '/models/ContentNode.php';
class  Model_Page extends Zend_Db_Table_Abstract{
	protected $_name = 'page';
	protected $dependentTable = array('Model_ContentNode');
	protected $_referenceMap = array(
		'Page'=> array(
			'columms'=> array('page_id'),
			'refTableClass' => 'Model_Page',
			'refColumms' => array('id'),
			'onDelete' => self::CASCADE,
			'onUpdate' =>self::RESTRICT
		)
	);
	public function updatePage($id, $data){
		$row = $this->find($id)->current();
		if($row){
			$row->name = $data['name'];
			$row->parent_id = $data['parent_id'];
			$row->save();
			unset($data['id']);
			unset($data['name']);
			unset($data['parent_id']);
			if(count($data) > 0){
				$mdlContentNode = new Model_ContentNode();
				foreach ($data as $key => $value){
					$mdlContentNode->setNode($id,$key,$value);
				}
			}
		}else{
			throw new Zend_Exception("could not open page");
		}
	}
	public function createPage($name, $namespage, $parentId = 0){
		$row = $this->createRow();
		$row->name = $name;
		$row->parent_id = $parentId;
		$row->date_created = time();
		$row->namespace=$namespage;
		$row->save();
		$id = $this->_db->lastInsertId();
		return $id;
	}
	public function deletePage($id){
		$row = $this->find(trim($id))->current();
		if($row){
			$row->delete();
			return true;
		}else{
			throw new Zend_Exception("delete function faild");
		}
	}
	public function getRecentPages($count = 10 , $namespace='page'){
		$select = $this->select();
		$select->order = 'date_created DESC';
		$select->where('namespace = ?', $namespace);
		$select->limit($count);
		$results = $this->fetchAll($select);
		if($results->count() > 0){
			$pages = array();
			foreach ($results as $result){
				$pages[$result->id] = new CMS_Content_Item_Page($result->id);
			}		
			return $pages;	
		}else{
			return null;
		}
	}
}





